home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / AppearanceFilter.java < prev    next >
Text File  |  1998-06-30  |  17KB  |  560 lines

  1. /*
  2.  * @(#)AppearanceFilter.java    1.2 98/01/30
  3.  *
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  *
  19.  */
  20.  
  21.  
  22. package com.sun.java.swing.plaf.mac;
  23.  
  24. import com.sun.java.swing.UIDefaults;
  25. import com.sun.java.swing.plaf.ColorUIResource;
  26. import java.awt.image.RGBImageFilter;
  27.  
  28. /*
  29.     AppearanceFilter
  30.  
  31.     Worker class for using the various appearance themes.
  32.  
  33.     Every appearance theme has a table of 8 colors that replace the various
  34.     grayscale colors.
  35.     
  36.     
  37.     Initialization:
  38.         call
  39.             AppearanceFilter.setAppearance("Lavender", uiDefaults); (or another name)
  40.     
  41.     
  42.     Simple Usage:
  43.         Load the images and colors that a component uses.
  44.         For images that need to be tinted with the accent colors,
  45.             for example the scrollbar thumb, use
  46.  
  47.             AppearanceFilter.FilterImageIcon(scrollbar,         // JComponent
  48.                                             scrollBoxEnabled);    // ImageIcon
  49.  
  50.         For colors that need to be tinted with the accent colors,
  51.             for example the progressbar content area, use
  52.  
  53.             progressBarInner1 = AppearanceFilter.FilterColor(UIManager.getColor("ProgressBar.inner1"));
  54.  
  55.  
  56.     Complex Usage:
  57.         For components that use different accent mappings for the grayscale colors,
  58.             you need to create your own subclass of AppearanceFilter that overrides
  59.             filterRGB(), and invoke its methods instead of the static AppearanceFilter methods.
  60.  
  61.         class MySpecialAppearanceFilter extends AppearanceFilter {
  62.             public int filterRGB(int x, int y, int rgb) {
  63.                 if (rgb == GS1)
  64.                     return appearance.getAppearanceColor(4);
  65.                 if (rgb == GS3)
  66.                     return appearance.getAppearanceColor(5);
  67.                 if (rgb == GS5)
  68.                     return appearance.getAppearanceColor(6);
  69.                 if (rgb == GS8)
  70.                     return appearance.getAppearanceColor(7);
  71.                 if (rgb == GS10)
  72.                     return appearance.getAppearanceColor(0);
  73.                 return rgb;
  74.             }
  75.  
  76.         MySpecialAppearanceFilter myAppearance = new MySpecialAppearanceFilter();
  77.  
  78.         myAppearance.FilterImageIcon(scrollbar,         // JComponent
  79.                                     scrollBoxEnabled);    // ImageIcon
  80.  
  81.     
  82.     T.B.D.: the black & white theme has no colors
  83. */
  84.  
  85.  
  86. public class AppearanceFilter extends java.awt.image.RGBImageFilter {
  87.     protected static final int GSB    = MacUtilities.GrayscaleAppearanceB.getRGB();
  88.     protected static final int GSW    = MacUtilities.GrayscaleAppearanceW.getRGB();
  89.     protected static final int GS1    = MacUtilities.GrayscaleAppearance1.getRGB();
  90.     protected static final int GS2    = MacUtilities.GrayscaleAppearance2.getRGB();
  91.     protected static final int GS3    = MacUtilities.GrayscaleAppearance3.getRGB();
  92.     protected static final int GS4    = MacUtilities.GrayscaleAppearance4.getRGB();
  93.     protected static final int GS5    = MacUtilities.GrayscaleAppearance5.getRGB();
  94.     protected static final int GS6    = MacUtilities.GrayscaleAppearance6.getRGB();
  95.     protected static final int GS7    = MacUtilities.GrayscaleAppearance7.getRGB();
  96.     protected static final int GS8    = MacUtilities.GrayscaleAppearance8.getRGB();
  97.     protected static final int GS9    = MacUtilities.GrayscaleAppearance9.getRGB();
  98.     protected static final int GS10    = MacUtilities.GrayscaleAppearance10.getRGB();
  99.     protected static final int GS11    = MacUtilities.GrayscaleAppearance11.getRGB();
  100.     protected static final int GS12    = MacUtilities.GrayscaleAppearance12.getRGB();
  101.     protected static final int GSA1    = MacUtilities.GrayscaleAppearanceA1.getRGB();
  102.     protected static final int GSA2    = MacUtilities.GrayscaleAppearanceA2.getRGB();
  103.  
  104.     
  105.     protected static Appearance appearance = null;
  106.     public static AppearanceFilter defaultAppearanceFilter = null;
  107.  
  108.     public AppearanceFilter() {
  109.         // The filter's operation does not depend on the
  110.         // pixel's location, so IndexColorModels can be
  111.         // filtered directly.
  112.         canFilterIndexColorModel = true;
  113.     }
  114.     
  115.     public static final void setAppearance(AppearanceFilter.Appearance appearance, UIDefaults table) {
  116.         AppearanceFilter.appearance = appearance;
  117.         if (defaultAppearanceFilter == null)
  118.             defaultAppearanceFilter = new AppearanceFilter();
  119.             
  120.         // Fill the ui defaults table with filtered grayscale colors
  121.         Object[] accentAppearanceColors =
  122.         {
  123.             "AccentAppearance1",        MacUtilities.GrayscaleAppearance1,
  124.             "AccentAppearance2",        MacUtilities.GrayscaleAppearance2,
  125.             "AccentAppearance3",        MacUtilities.GrayscaleAppearance3,
  126.             "AccentAppearance4",        MacUtilities.GrayscaleAppearance4,
  127.             "AccentAppearance5",        MacUtilities.GrayscaleAppearance5,
  128.             "AccentAppearance6",        MacUtilities.GrayscaleAppearance6,
  129.             "AccentAppearance7",        MacUtilities.GrayscaleAppearance7,
  130.             "AccentAppearance8",        MacUtilities.GrayscaleAppearance8,
  131.             "AccentAppearance9",        MacUtilities.GrayscaleAppearance9,
  132.             "AccentAppearance10",        MacUtilities.GrayscaleAppearance10,
  133.             "AccentAppearance11",        MacUtilities.GrayscaleAppearance11,
  134.             "AccentAppearance12",        MacUtilities.GrayscaleAppearance12,
  135.             "AccentAppearanceA1",        MacUtilities.GrayscaleAppearanceA1,
  136.             "AccentAppearanceA2",        MacUtilities.GrayscaleAppearanceA2,
  137.         };
  138.  
  139.         for(int i = 0; i < accentAppearanceColors.length; i += 2)
  140.             table.put((String) accentAppearanceColors[i], new ColorUIResource(AppearanceFilter.FilterRGB(((java.awt.Color) accentAppearanceColors[i + 1]).getRGB())));
  141.     }
  142.  
  143.     public static void setAppearance(String appearanceName, UIDefaults table) {
  144.         AppearanceFilter.Appearance appearance;
  145.  
  146.         try {
  147.             String classname = "com.sun.java.swing.plaf.mac.AppearanceFilter$" + appearanceName.replace(' ', '_') + "Appearance";
  148.             java.lang.Class c = java.lang.Class.forName(classname);
  149.             appearance = (AppearanceFilter.Appearance) c.newInstance();
  150.         }
  151.         catch (Exception e) {
  152.             e.printStackTrace();
  153.             appearance = new AppearanceFilter.LavenderAppearance();
  154.         }
  155.         
  156.         AppearanceFilter.setAppearance(appearance, table);
  157.     }
  158.     
  159.     public static final void FilterImageIcon(java.awt.Component imageComponent, com.sun.java.swing.ImageIcon imageIcon) {
  160.         defaultAppearanceFilter.filterImageIcon(imageComponent, imageIcon);
  161.     }
  162.     
  163.     public static final java.awt.Image FilterImage(java.awt.Component imageComponent, java.awt.Image image) {
  164.         return defaultAppearanceFilter.filterImage(imageComponent, image);
  165.     }
  166.     
  167.     public static final java.awt.Color FilterColor(java.awt.Color color) {
  168.         return defaultAppearanceFilter.filterColor(color);
  169.     }
  170.     public static final int FilterRGB(int rgb) {
  171.         return defaultAppearanceFilter.filterRGB(rgb);
  172.     }
  173.  
  174.  
  175.     public final void filterImageIcon(java.awt.Component imageComponent, com.sun.java.swing.ImageIcon imageIcon) {
  176.         imageIcon.setImage(this.filterImage(imageComponent, imageIcon.getImage()));
  177.     }
  178.     
  179.     public final java.awt.Image filterImage(java.awt.Component imageComponent, java.awt.Image image) {
  180.         return imageComponent.createImage(new java.awt.image.FilteredImageSource(image.getSource(), this));
  181.     }
  182.     
  183.     public final java.awt.Color filterColor(java.awt.Color color) {
  184.         return new java.awt.Color(this.filterRGB(0, 0, color.getRGB()));
  185.     }
  186.     public final int filterRGB(int rgb) {
  187.         return this.filterRGB(0, 0, rgb);
  188.     }
  189.  
  190.  
  191.     public int filterRGB(int x, int y, int rgb) {
  192.         if (rgb == GS1)
  193.             return appearance.getAppearanceColor(0);
  194.         if (rgb == GS3)
  195.             return appearance.getAppearanceColor(1);
  196.         if (rgb == GS5)
  197.             return appearance.getAppearanceColor(2);
  198.         if (rgb == GS8)
  199.             return appearance.getAppearanceColor(3);
  200.         if (rgb == GS10)
  201.             return appearance.getAppearanceColor(4);
  202.         if (rgb == GS12)
  203.             return appearance.getAppearanceColor(5);
  204.         return rgb;
  205.     }
  206.  
  207.  
  208.     public interface Appearance {
  209.         public int getAppearanceColor(int index);
  210.     }
  211.  
  212.     public interface AquamarineAppearanceColors {
  213.         static final int aquamarineColors[] = new int[] {
  214.                                                             0xffccccff,
  215.                                                             0xff66ffcc,
  216.                                                             0xff00cc99,
  217.                                                             0xff009999,
  218.                                                             0xff006666,
  219.                                                             0xff003333,
  220.                                                             0xff002200,
  221.                                                             0xff000000
  222.                                                         };
  223.     }
  224.  
  225.     public interface CopperAppearanceColors {
  226.         static final int copperColors[] = new int[] {
  227.                                                             0xffffffcc,
  228.                                                             0xffffcc99,
  229.                                                             0xffff9966,
  230.                                                             0xffcc6633,
  231.                                                             0xff993300,
  232.                                                             0xff660000,
  233.                                                             0xff220000,
  234.                                                             0xff000000
  235.                                                         };
  236.     }
  237.  
  238.     public interface CrimsonAppearanceColors {
  239.         static final int crimsonColors[] = new int[] {
  240.                                                             0xffffcccc,
  241.                                                             0xffff9999,
  242.                                                             0xffff6666,
  243.                                                             0xffcc3333,
  244.                                                             0xff990000,
  245.                                                             0xff770000,
  246.                                                             0xff440000,
  247.                                                             0xff000000
  248.                                                         };
  249.     }
  250.  
  251.     public interface EmeraldAppearanceColors {
  252.         static final int emeraldColors[] = new int[] {
  253.                                                             0xffccffcc,
  254.                                                             0xff66ff99,
  255.                                                             0xff33cc66,
  256.                                                             0xff339966,
  257.                                                             0xff006633,
  258.                                                             0xff004400,
  259.                                                             0xff002200,
  260.                                                             0xff000000
  261.                                                         };
  262.     }
  263.  
  264.     public interface French_BlueAppearanceColors {
  265.         static final int frenchBlueColors[] = new int[] {
  266.                                                             0xffeeeeee,
  267.                                                             0xffccccff,
  268.                                                             0xff9999cc,
  269.                                                             0xff666699,
  270.                                                             0xff333366,
  271.                                                             0xff000022,
  272.                                                             0xff000011,
  273.                                                             0xff000000
  274.                                                         };
  275.     }
  276.  
  277.     public interface GoldAppearanceColors {
  278.         static final int goldColors[] = new int[] {
  279.                                                             0xffffff99,
  280.                                                             0xffffff00,
  281.                                                             0xffcccc00,
  282.                                                             0xff999900,
  283.                                                             0xff666600,
  284.                                                             0xff333300,
  285.                                                             0xff111111,
  286.                                                             0xff000000
  287.                                                         };
  288.     }
  289.  
  290.     public interface IvyAppearanceColors {
  291.         static final int ivyColors[] = new int[] {
  292.                                                             0xffccffcc,
  293.                                                             0xff99cc99,
  294.                                                             0xff669966,
  295.                                                             0xff336633,
  296.                                                             0xff003300,
  297.                                                             0xff002200,
  298.                                                             0xff001100,
  299.                                                             0xff000000
  300.                                                         };
  301.     }
  302.  
  303.     public interface LavenderAppearanceColors {
  304.         static final int lavenderColors[] = new int[] {
  305.                                                             0xffeeeeee,
  306.                                                             0xffccccff,
  307.                                                             0xff9999ff,
  308.                                                             0xff6666cc,
  309.                                                             0xff333399,
  310.                                                             0xff000088,
  311.                                                             0xff000055,
  312.                                                             0xff000000
  313.                                                         };
  314.     }
  315.  
  316.     public interface LimeAppearanceColors {
  317.         static final int limeColors[] = new int[] {
  318.                                                             0xffffffcc,
  319.                                                             0xffccff66,
  320.                                                             0xff99cc66,
  321.                                                             0xff669900,
  322.                                                             0xff336600,
  323.                                                             0xff004400,
  324.                                                             0xff003300,
  325.                                                             0xff000000
  326.                                                         };
  327.     }
  328.  
  329.     public interface MagentaAppearanceColors {
  330.         static final int magentaColors[] = new int[] {
  331.                                                             0xffffccff,
  332.                                                             0xffff99ff,
  333.                                                             0xffcc66cc,
  334.                                                             0xff993399,
  335.                                                             0xff660066,
  336.                                                             0xff330033,
  337.                                                             0xff220000,
  338.                                                             0xff000000
  339.                                                         };
  340.     }
  341.  
  342.     public interface NutmegAppearanceColors {
  343.         static final int nutmegColors[] = new int[] {
  344.                                                             0xffffffcc,
  345.                                                             0xffffcc99,
  346.                                                             0xffcc9966,
  347.                                                             0xff996633,
  348.                                                             0xff663300,
  349.                                                             0xff220000,
  350.                                                             0xff110000,
  351.                                                             0xff000000
  352.                                                         };
  353.     }
  354.  
  355.     public interface OliveAppearanceColors {
  356.         static final int oliveColors[] = new int[] {
  357.                                                             0xffffffcc,
  358.                                                             0xffcccc99,
  359.                                                             0xff999966,
  360.                                                             0xff666633,
  361.                                                             0xff333300,
  362.                                                             0xff002200,
  363.                                                             0xff001100,
  364.                                                             0xff000000
  365.                                                         };
  366.     }
  367.  
  368.     public interface PlumAppearanceColors {
  369.         static final int plumColors[] = new int[] {
  370.                                                             0xffffccff,
  371.                                                             0xffcc99cc,
  372.                                                             0xff996699,
  373.                                                             0xff663366,
  374.                                                             0xff330033,
  375.                                                             0xff220000,
  376.                                                             0xff110000,
  377.                                                             0xff000000
  378.                                                         };
  379.     }
  380.     public interface RoseAppearanceColors {
  381.         static final int roseColors[] = new int[] {
  382.                                                             0xffeeeeee,
  383.                                                             0xffffcccc,
  384.                                                             0xffcc9999,
  385.                                                             0xff996666,
  386.                                                             0xff663333,
  387.                                                             0xff440000,
  388.                                                             0xff220000,
  389.                                                             0xff000000
  390.                                                         };
  391.     }
  392.     public interface SapphireAppearanceColors {
  393.         static final int sapphireColors[] = new int[] {
  394.                                                             0xffeeeeee,
  395.                                                             0xff99ccff,
  396.                                                             0xff6699ff,
  397.                                                             0xff3366ff,
  398.                                                             0xff0033cc,
  399.                                                             0xff000099,
  400.                                                             0xff000055,
  401.                                                             0xff000000
  402.                                                         };
  403.     }
  404.     public interface SilverAppearanceColors {
  405.         static final int silverColors[] = new int[] {
  406.                                                             0xffeeeeee,
  407.                                                             0xffcccccc,
  408.                                                             0xffaaaaaa,
  409.                                                             0xff777777,
  410.                                                             0xff555555,
  411.                                                             0xff333333,
  412.                                                             0xff222222,
  413.                                                             0xff000000
  414.                                                         };
  415.     }
  416.     public interface TealAppearanceColors {
  417.         static final int tealColors[] = new int[] {
  418.                                                             0xffeeeeee,
  419.                                                             0xff99cccc,
  420.                                                             0xff669999,
  421.                                                             0xff336666,
  422.                                                             0xff003333,
  423.                                                             0xff002200,
  424.                                                             0xff001100,
  425.                                                             0xff000000
  426.                                                         };
  427.     }
  428.     public interface TurquoiseAppearanceColors {
  429.         static final int turquoiseColors[] = new int[] {
  430.                                                             0xffccffff,
  431.                                                             0xff66ffff,
  432.                                                             0xff00ccff,
  433.                                                             0xff0099cc,
  434.                                                             0xff006699,
  435.                                                             0xff003366,
  436.                                                             0xff000022,
  437.                                                             0xff000000
  438.                                                         };
  439.     }
  440.  
  441.     public static class AquamarineAppearance implements Appearance, AquamarineAppearanceColors
  442.     {
  443.         public int getAppearanceColor(int index) {
  444.             return aquamarineColors[index];
  445.         }
  446.     }
  447.  
  448.     public static class CopperAppearance implements Appearance, CopperAppearanceColors
  449.     {
  450.         public int getAppearanceColor(int index) {
  451.             return copperColors[index];
  452.         }
  453.     }
  454.  
  455.     public static class CrimsonAppearance implements Appearance, CrimsonAppearanceColors
  456.     {
  457.         public int getAppearanceColor(int index) {
  458.             return crimsonColors[index];
  459.         }
  460.     }
  461.  
  462.     public static class EmeraldAppearance implements Appearance, EmeraldAppearanceColors
  463.     {
  464.         public int getAppearanceColor(int index) {
  465.             return emeraldColors[index];
  466.         }
  467.     }
  468.  
  469.     public static class French_BlueAppearance implements Appearance, French_BlueAppearanceColors
  470.     {
  471.         public int getAppearanceColor(int index) {
  472.             return frenchBlueColors[index];
  473.         }
  474.     }
  475.  
  476.     public static class GoldAppearance implements Appearance, GoldAppearanceColors
  477.     {
  478.         public int getAppearanceColor(int index) {
  479.             return goldColors[index];
  480.         }
  481.     }
  482.  
  483.     public static class IvyAppearance implements Appearance, IvyAppearanceColors
  484.     {
  485.         public int getAppearanceColor(int index) {
  486.             return ivyColors[index];
  487.         }
  488.     }
  489.  
  490.     public static class LavenderAppearance implements Appearance, LavenderAppearanceColors
  491.     {
  492.         public int getAppearanceColor(int index) {
  493.             return lavenderColors[index];
  494.         }
  495.     }
  496.  
  497.     public static class LimeAppearance implements Appearance, LimeAppearanceColors
  498.     {
  499.         public int getAppearanceColor(int index) {
  500.             return limeColors[index];
  501.         }
  502.     }
  503.  
  504.     public static class MagentaAppearance implements Appearance, MagentaAppearanceColors
  505.     {
  506.         public int getAppearanceColor(int index) {
  507.             return magentaColors[index];
  508.         }
  509.     }
  510.  
  511.     public static class NutmegAppearance implements Appearance, NutmegAppearanceColors
  512.     {
  513.         public int getAppearanceColor(int index) {
  514.             return nutmegColors[index];
  515.         }
  516.     }
  517.     public static class OliveAppearance implements Appearance, OliveAppearanceColors
  518.     {
  519.         public int getAppearanceColor(int index) {
  520.             return oliveColors[index];
  521.         }
  522.     }
  523.     public static class PlumAppearance implements Appearance, PlumAppearanceColors
  524.     {
  525.         public int getAppearanceColor(int index) {
  526.             return plumColors[index];
  527.         }
  528.     }
  529.     public static class RoseAppearance implements Appearance, RoseAppearanceColors
  530.     {
  531.         public int getAppearanceColor(int index) {
  532.             return roseColors[index];
  533.         }
  534.     }
  535.     public static class SapphireAppearance implements Appearance, SapphireAppearanceColors
  536.     {
  537.         public int getAppearanceColor(int index) {
  538.             return sapphireColors[index];
  539.         }
  540.     }
  541.     public static class SilverAppearance implements Appearance, SilverAppearanceColors
  542.     {
  543.         public int getAppearanceColor(int index) {
  544.             return silverColors[index];
  545.         }
  546.     }
  547.     public static class TealAppearance implements Appearance, TealAppearanceColors
  548.     {
  549.         public int getAppearanceColor(int index) {
  550.             return tealColors[index];
  551.         }
  552.     }
  553.     public static class TurquoiseAppearance implements Appearance, TurquoiseAppearanceColors
  554.     {
  555.         public int getAppearanceColor(int index) {
  556.             return turquoiseColors[index];
  557.         }
  558.     }
  559. }
  560.